﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace School.SystemManage.WebPages.Contact
{
    public partial class ClassWeibo : Base.PermissionBasePage
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            var data = db.Weibo;
            lv_list.DataSource = data;
            lv_list.DataBind();
        }

        protected void lv_list_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            lv_list.EditIndex = -1;
            BindData();
        }

        protected void lv_list_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            int kid = (int)lv_list.DataKeys[e.ItemIndex].Value;
            Weibo weibo = db.Weibo.FirstOrDefault(w => w.WID == kid);

            db.DeleteObject(weibo);
            db.SaveChanges();

            BindData();
        }

        protected void lv_list_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            int kid = (int)lv_list.DataKeys[e.ItemIndex].Value;
            Weibo weibo = db.Weibo.FirstOrDefault(w => w.WID == kid);

            ListViewItem item = lv_list.Items[e.ItemIndex];
            DropDownList _ddlMajor = item.FindControl("ddlMajor") as DropDownList;
            DropDownList _ddlGrade = item.FindControl("ddlGrade") as DropDownList;
            TextBox _txbClassName=item.FindControl("txbClassName") as TextBox;
            TextBox _txbUrl=item.FindControl("txbUrl") as TextBox;
            RadioButtonList _rbtLinked = item.FindControl("rbtIsLinked") as RadioButtonList;

            weibo.Major =byte.Parse( _ddlMajor.SelectedValue);
            weibo.Grade = byte.Parse(_ddlGrade.SelectedValue);
            weibo.ClassName = _txbClassName.Text.Trim();
            weibo.Url = _txbUrl.Text.Trim();
            weibo.IsLinked = _rbtLinked.SelectedValue == "1";

            db.SaveChanges();

            lv_list.EditIndex = -1;
            BindData();
        }

        protected void lv_list_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            lv_list.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void lv_list_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                Weibo weibo = (e.Item as ListViewDataItem).DataItem as School.Weibo;

                DropDownList ddlMajor = e.Item.FindControl("ddlMajor") as DropDownList;
                DropDownList ddlGrade = e.Item.FindControl("ddlGrade") as DropDownList;
                RadioButtonList rbtLinked = e.Item.FindControl("rbtIsLinked") as RadioButtonList;
                if (ddlMajor != null) ddlMajor.SelectedIndex = int.Parse(weibo.Major.Value.ToString());
                if (ddlGrade != null) ddlGrade.SelectedIndex = int.Parse(weibo.Grade.Value.ToString()) - 1;
                if (rbtLinked != null) rbtLinked.SelectedIndex = weibo.IsLinked == true ? 1 : 0;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                Weibo newItem = new Weibo();
                newItem.Major = byte.Parse(ddlMajor.SelectedValue);
                newItem.Grade = byte.Parse(ddlGrade.SelectedValue);
                newItem.ClassName = txbClassName.Text.Trim();
                newItem.Url = txbUrl.Text.Trim();
                newItem.IsLinked = rbtIsLinked.SelectedValue == "1";

                db.AddToWeibo(newItem);
                if (db.SaveChanges() > 0)
                {
                    lblMsg.Text = "添加成功！";
                    BindData();
                }
                else
                {
                    lblMsg.Text = "添加失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "添加发生异常，原因：" + ex.Message;
            }
        }

        protected string GetMajor(string code)
        {
            string major = string.Empty;
            switch (code)
            { 
                case "0":
                    major = "会计";
                    break;
                case "1":
                    major = "国贸";
                    break;
                case "2":
                    major = "营销";
                    break;
                case "3":
                    major = "国商";
                    break;
                default:
                    break;
            }
            return major;
        }

        protected string GetGrade(string code)
        {
            string grade = string.Empty;
            switch (code)
            {
                case "1":
                    grade = "一年级";
                    break;
                case "2":
                    grade = "二年级";
                    break;
                case "3":
                    grade = "三年级";
                    break;
                case "4":
                    grade = "四年级";
                    break;
                default:
                    break;
            }
            return grade;
        }

        #region 操作权限控制

        public override void SetEditDisable()
        {
            base.SetEditDisable();

            foreach (ListViewDataItem item in lv_list.Items)
            {
                if (item.ItemType == ListViewItemType.DataItem)
                {
                    LinkButton linkEdit = (LinkButton)item.FindControl("LinkButton1");
                    LinkButton linkDelete = (LinkButton)item.FindControl("LinkButton2");
                    linkEdit.Visible = false;
                    linkDelete.Visible = false;
                }
            }
            addTable.Visible = false;
        }

        public override void SetReviewDisable()
        {
            base.SetReviewDisable();
        }

        public override void SetViewDisable()
        {
            base.SetViewDisable();
        }

        #endregion
        
    }
}
